package springcloud.easybuy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import springcloud.easybuy.api.EasybuyProductManagerClient;
import springcloud.easybuy.api.ProductCategoryManagerClient;
import springcloud.easybuy.entity.EasybuyProduct;
import springcloud.easybuy.entity.EasybuyProductCategory;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InitController {

    @Resource
    private EasybuyProductManagerClient easybuyProductManagerClient;


    @Resource
    private ProductCategoryManagerClient productCategoryManagerClient;

    @RequestMapping("/init")
    public String init(ModelMap map){
        List<Object> list = new ArrayList<>();

        Map<Integer,List<EasybuyProduct>> map1 = new HashMap<>();
        List<EasybuyProductCategory> queryAllCategory = productCategoryManagerClient.queryAllProduct();
        List<EasybuyProductCategory> parentProductCategory = new ArrayList<>();
        List<EasybuyProduct> list1 = easybuyProductManagerClient.queryProduct();
        for (EasybuyProductCategory easybuyProductCategory : queryAllCategory){
            if (easybuyProductCategory.getParentId() == 0){
                parentProductCategory.add(easybuyProductCategory);
                List<EasybuyProduct> easybuyProductList= easybuyProductManagerClient.queryProductById(easybuyProductCategory.getId());
                map1.put(easybuyProductCategory.getId(),easybuyProductList);
            }
        }
        map.addAttribute("allInfo",map1);
        map.addAttribute("parent",parentProductCategory);
        map.addAttribute("all",list1);
        return "/pre/index";
    }


    @RequestMapping("/goodsdetail")
    public String getGoodsDetail(HttpServletRequest request,ModelMap map){
        Integer id = Integer.parseInt(request.getParameter("id"));
        System.out.println("id"+id);
        EasybuyProduct easybuyProduct = easybuyProductManagerClient.findByMainId(id);
        map.addAttribute("good",easybuyProduct);
        return "/pre/product/productDetail";
    }

}
